//
//  MBDEarnIntegrationViewController.swift
//  MasterBoy
//
//  Created by kyson on 2023/4/8.
//

import Foundation
import UIKit
import YYText
import JLRoutes
//import GoogleMobileAds
import Toast_Swift

class MBDEarnIntegrationCellModel : MBDCommonCellModel {
    var nextVCURLString : String?
    var title : String?
    var location : MBDCellLocation?
    
    override func cellHeight() -> CGFloat {
        return 55
    }
}

class MBDEarnIntegrationTableViewModel : NSObject {
    var cellModels : Array<MBDEarnIntegrationCellModel>!
    override init() {
        var cellModels =  [MBDEarnIntegrationCellModel]()
        
        let cellModel = MBDEarnIntegrationCellModel()
        cellModel.title = "完善个人信息"
        cellModel.nextVCURLString = "personalInfo"
        cellModel.location = .top
        cellModels.append(cellModel)
        
        let cellModel2 = MBDEarnIntegrationCellModel()
        cellModel2.title = "发布新群"
        cellModel2.nextVCURLString = "uploadGroup"
        cellModels.append(cellModel2)
        
        let cellModel3 = MBDEarnIntegrationCellModel()
        cellModel3.title = "看广告"
        cellModel3.nextVCURLString = "lookAd"
        cellModel3.location = .bottom

        cellModels.append(cellModel3)
        
        self.cellModels = cellModels
    }
}


class MBDEarnIntegrationViewController : MBDBaseViewController {
    
    var tableView : UITableView!
    var tableViewModel : MBDEarnIntegrationTableViewModel!
    var adManager : MBDShowAdManager?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "赚取积分"
        
        tableViewModel = MBDEarnIntegrationTableViewModel()
        
        JLRoutes.global().addRoute("lookAd", handler : { _ in NSDictionary.self
            self.adManager = MBDShowAdManager()
//            self.adManager!.showAnd(targetVC: self)
            
            return true
        })
        
        tableView = UITableView()
        tableView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
        tableView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(tableView)
        
        NSLayoutConstraint.activate([
        self.view.safeAreaLayoutGuide.topAnchor.constraint(equalTo: tableView.topAnchor),
                    self.view.bottomAnchor.constraint(equalTo: tableView.bottomAnchor),
                    self.view.leadingAnchor.constraint(equalTo: tableView.leadingAnchor),
                    self.view.trailingAnchor.constraint(equalTo: tableView.trailingAnchor),
                ])
        tableView.backgroundColor = UIColor(colorHex: 0xF4F4F4)
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.estimatedRowHeight = 10
        
        tableView.register(MBDEarnIntegrateCell.self, forCellReuseIdentifier: "MBDEarnIntegrateCell")

        self.view.setDynamicBackgroundColor()
        self.tableView.setDynamicBackgroundColor()
    }
    
    override func updateColors() {
        self.view.setDynamicBackgroundColor()
        tableView.setDynamicBackgroundColor()
        self.tableView.reloadData()
    }
    
    /// Tells the delegate that the ad failed to present full screen content.
     
//    func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
//        
//        print("Ad did fail to present full screen content.")
//    }
////
////      /// Tells the delegate that the ad will present full screen content.
//    func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
//     
//        print("Ad will present full screen content.")
//    }
////
////      /// Tells the delegate that the ad dismissed full screen content.
//    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
//      
//        print("Ad did dismiss full screen content.")
//            
//        MBDHttpClient.shared.doPostRequest("/userIntegral/save").httpClientResponse { error, response in
//            if nil != error {
//                self.view.makeToast(error!.localizedDescription)
//            } else {
//                self.view.makeToast("积分获取成功!")
//
//            }
//        }
//        
//    }

    
    deinit {
        // perform the deinitialization
        debugPrint("deinit")
    }
    
}



extension MBDEarnIntegrationViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tableViewModel.cellModels?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellModel = self.tableViewModel.cellModels![indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "MBDEarnIntegrateCell", for: indexPath)
        (cell as! MBDEarnIntegrateCell).cellModel = cellModel
        return cell
    }
}


extension MBDEarnIntegrationViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let cellModel = self.tableViewModel.cellModels![indexPath.row]
        JLRoutes.routeURL(URL(string: cellModel.nextVCURLString ?? ""))
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let cellModel = self.tableViewModel.cellModels![indexPath.row]

        return cellModel.cellHeight()
    }
}


class MBDEarnIntegrateCell: MBDCommonCell {

    var titleLabel : YYLabel!
    var modifyLabel : YYLabel!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        self.initialize()
    }
    
    func initialize() {
        contentView.setDynamicForgroundColor()
        commonBackgroundView.setDynamicForgroundColor()
        
        titleLabel = YYLabel()
        self.commonBackgroundView.addSubview(titleLabel)
        
        modifyLabel = YYLabel()
        self.commonBackgroundView.addSubview(modifyLabel)
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(self.commonBackgroundView)
            make.left.equalTo(commonBackgroundView).offset(8)
        }
        
        modifyLabel.snp.makeConstraints { make in
            make.centerY.equalTo(self.commonBackgroundView)
            make.right.equalTo(self.rightArrowImage.snp.left).offset(-10)
        }

    }
    
    override var cellModel: Any! {
        didSet {
            let realCellModel = cellModel as? MBDEarnIntegrationCellModel
            self.title = realCellModel?.title
            self.location = realCellModel?.location
            self.accessable = true
//            self.detailTobeModify =
        }
    }
    
    var title : String? {
        didSet {
            
            let usernameAttributeString = NSMutableAttributedString(string: self.title ?? "")
            usernameAttributeString.yy_font = UIFont.systemFont(ofSize: 14)
            usernameAttributeString.setDynamicTextColor()
            
            titleLabel.attributedText = usernameAttributeString
        }
        
        willSet {
        }
    }
    
    var detailTobeModify : String? {
        didSet {
            
            let usernameAttributeString = NSMutableAttributedString(string: self.detailTobeModify ?? "")
            usernameAttributeString.yy_font = UIFont.systemFont(ofSize: 14)
            usernameAttributeString.setDynamicTextColor()
            
            modifyLabel.attributedText = usernameAttributeString
        }
        
        willSet {
        }
    }
    
}
